package com.example.zce.back.controller.information;

import com.example.zce.library.common.bean.ResultMapBean;
import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.data.information.entity.InformationNews;
import com.example.zce.library.data.information.service.InformationNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/8/8 14:01
 */
@Controller
@RequestMapping("/information/news")
public class NewsController {

    @Autowired
    private InformationNewsService informationNewsService;


    /**
     * 首页
     * @return
     */
    @GetMapping("/index.html")
    public ModelAndView indexHtml(){
        return new ModelAndView("information/news-index");
    }

    /**
     * 分页数据
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/list.json")
    public ResultTableBean<InformationNews> listJson(HttpServletRequest request){
        Map params=new HashMap();
        params.put("pageNum",request.getParameter("page"));
        params.put("pageSize",request.getParameter("limit"));

        ResultTableBean<InformationNews> resultTableBean = informationNewsService.findByParams(params);
        return resultTableBean;
    }

    /**
     * 添加页面
     * @return
     */
    @GetMapping("/add.html")
    public ModelAndView addHtml(){
        return new ModelAndView("information/news-add");
    }

    @ResponseBody
    @RequestMapping("/saveupdate.json")
    @PostMapping
    public ResultMapBean saveupdateJson(HttpServletRequest request,InformationNews informationNews){
        try {
            int i = informationNewsService.saveOrUpdate(informationNews);
            if (i!=0){
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

}
